package com.melvin.common;

import java.util.Random;
import java.util.concurrent.Callable;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;

public class UtilEntry {
    public static void main(String[] args) {
        ExecutorService executorService1 = Executors.newSingleThreadExecutor();
        ExecutorService executorService2 = Executors.newCachedThreadPool();
        int i = 0;
        while (++i<100){
            MyRunnable a1 = new MyRunnable("a"+i);
            MyRunnable b1 = new MyRunnable("b"+i);
            //executorService.execute(a1);
            //executorService.execute(b1);
            new Thread(a1).start();

            Future<Long> f = executorService2.submit(new MyCallable());
            try {
                f.get();
            } catch (InterruptedException | ExecutionException e) {
                e.printStackTrace();
            }finally {
                executorService2.shutdown();
            }
        }
        executorService1.shutdown();
        executorService2.shutdown();
    }

    static class MyRunnable implements Runnable{
        private String name;
        MyRunnable(String name){
            this.name = name;
        }
        @Override
        public void run() {
            System.out.print(name);
        }
    }

    static class MyCallable implements Callable<Long>{

        @Override
        public Long call() throws Exception {
            return System.currentTimeMillis();
        }
    }

    public void printlnSomething(){
        System.out.println("random:"+new Random().nextGaussian());
    }
}
